﻿using System.Collections.Concurrent;

namespace PLC.Domain.ScheduledTasks;
/// <summary>
/// task的状态管理类  ture表示正在运行  false表示没有在运行
/// </summary>
public class TaskStatus
{
    private static ConcurrentDictionary<long, bool> _taskStatusDic = new ConcurrentDictionary<long, bool>();

    /// <summary>
    /// 删除对应的key
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public static bool Remove(long key)
    {
        return _taskStatusDic.TryRemove(key, out var value);
    }
    /// <summary>
    /// 添加 如果存在就修改
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool AddOrUpdate(long key, bool value)
    {
        return _taskStatusDic.AddOrUpdate(key, value, (updateKye, oldVlue) => value);
    }
    /// <summary>
    /// 查询 查询不到就添加
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool GetOrAdd(long key, bool value)
    {
        return _taskStatusDic.GetOrAdd(key, value);
    }
}